﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;
using System.Data;
using RuleCode.Common;

namespace map.member
{
    public partial class searchFriends : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NoFriendsDataDiv.Style["diplay"] = "block";
            if (!IsPostBack)
            {
                InitStatesSelects();
                BindMainWebPage();
            }
        }


        #region 辅助方法

        private void InitStatesSelects()
        {
            this.StateDropDownList.Items.Clear();
            ListItem listItem = new ListItem("家乡省份", "");
            listItem.Selected = true;
            this.StateDropDownList.Items.Add(listItem);
            taobaostate state = taobaostate.Find("taobaostateid!='海外'");
            if (state.Count > 0)
            {
                for (int i = 0; i < state.Count; i++)
                {
                    listItem = new ListItem(state.taobaostateid.Value, state.taobaostateid.Value);
                    this.StateDropDownList.Items.Add(listItem);
                    state.Next();
                }
            }
            StateDropDownList_SelectedIndexChanged(this.StateDropDownList, EventArgs.Empty);
        }

        /// <summary>
        /// 一般关键字搜索
        /// </summary>
        /// <param name="searchKeys"></param>
        /// <param name="userSexRadio"></param>
        /// <param name="StateSelect"></param>
        /// <param name="CitySelect"></param>
        /// <param name="collageName"></param>
        private void NormalSearch(string searchKeys, string userSexRadio, string StateSelect, string CitySelect, string collageName)
        {
            NoFriendsDataDiv.Style["diplay"] = "block";
            this.searchKeysInputText.Value = searchKeys;

            string SqlStr = string.Empty;
            SqlStr = "exec SearchRelationFriends @userid='"+AliApi.appUserId +"'"
                    + ",@searchKeys='" + searchKeys + "'"
                    + ",@userSex='" + userSexRadio + "'"
                    + ",@State='" + StateSelect + "'"
                    + ",@City='" + CitySelect + "'"
                    + ",@collageName='" + collageName + "'";
            DataSet dSet = SqlHelper.Instance.ExecuteQuery(SqlStr);
            if (dSet.Tables.Count > 0)
            {
                if(dSet.Tables [0].Rows.Count > 0)
                    NoFriendsDataDiv.Style["diplay"] = "none";
                MainWebPager.DataSource = dSet.Tables[0];
                MainWebPager.DataBind();
            }
        }

        /// <summary>
        /// 复杂的高级搜索
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userSexRadio"></param>
        /// <param name="StateSelect"></param>
        /// <param name="CitySelect"></param>
        /// <param name="EmailAddress"></param>
        /// <param name="QQNumber"></param>
        /// <param name="MSNNumber"></param>
        /// <param name="collageName"></param>
        /// <param name="collageYearSelect"></param>
        /// <param name="sSchoolName"></param>
        /// <param name="sSchoolYearSelect"></param>
        /// <param name="jSchoolName"></param>
        /// <param name="jSchoolYearSelect"></param>
        /// <param name="lastEmployeeName"></param>
        private void AdvanceSearch(string userName, string userSexRadio, string StateSelect, string CitySelect,
            string EmailAddress , string QQNumber, string MSNNumber, string collageName, string collageYearSelect,
            string sSchoolName, string sSchoolYearSelect, string jSchoolName, string jSchoolYearSelect,
            string lastEmployeeName)
        {
            NoFriendsDataDiv.Style["diplay"] = "block";

            string SqlStr = string.Empty;
            SqlStr = "exec SearchRelationFriends @userid='" + AliApi.appUserId + "'"
                    + ",@userName='" + userName + "'"
                    + ",@userSex='" + userSexRadio + "'"
                    + ",@State='" + StateSelect + "'"
                    + ",@City='" + CitySelect + "'"
                    + ",@EmailAddress='" + EmailAddress + "'"
                    + ",@QQNumber='" + QQNumber + "'"
                    + ",@MSNNumber='" + MSNNumber + "'"
                    + ",@collageName='" + collageName + "'"
                    + ",@collageYear=" + collageYearSelect
                    + ",@sSchoolName='" + sSchoolName + "'"
                    + ",@sSchoolYear=" + sSchoolYearSelect 
                    + ",@jSchoolName='" + jSchoolName + "'"
                    + ",@jSchoolYear=" + jSchoolYearSelect 
                    + ",@lastEmployeeName='" + lastEmployeeName + "'";
            DataSet dSet = SqlHelper.Instance.ExecuteQuery(SqlStr);
            if (dSet.Tables.Count > 0)
            {
                if (dSet.Tables[0].Rows.Count > 0)
                    NoFriendsDataDiv.Style["diplay"] = "none";
                MainWebPager.DataSource = dSet.Tables[0];
                MainWebPager.DataBind();
            }
        }

        public string GetUlHtml(string name, string collageName, string jSchoolName, string sSchoolName, string lastEmployeeName)
        {
            string result = string.Empty;
            result += "<ul>";

            if (name != string.Empty)
                result += "<li>姓名：" + name + "</li>";
            if (jSchoolName != string.Empty)
                result += "<li style=\"padding-left:30px\" title =\"初中\">      " + jSchoolName + "</li>";
            if (sSchoolName != string.Empty)
                result += "<li style=\"padding-left:30px\" title =\"高中\">      " + sSchoolName + "</li>";
            if (collageName != string.Empty)
                result += "<li style=\"padding-left:30px\" title =\"大学\">      " + collageName + "</li>";
            if (lastEmployeeName != string.Empty)
                result += "<li style=\"padding-left:30px\" title =\"曾经工作单位\">      " + lastEmployeeName + "</li>";
            result += "</ul>";
            return result;
        }

        private void BindMainWebPage()
        {

            string searchType = GetParameters("searchType");// Request.Form["searchType"];
            if (searchType.ToUpper() != "1")  //普通搜索
            {
                string searchKeys = GetParameters("searchkeys");
                //if (searchType == "0")
                {
                    if (this.searchKeysInputText.Value != "可输入姓名 学校 单位")
                        searchKeys = searchKeysInputText.Value;

                    string collageName = string.Empty;
                    string userSex = string.Empty;
                    string StateSelect = string.Empty;
                    string CitySelect = string.Empty;

                    if (this.CollageTextInput.Value != "请输入大学")
                        collageName = this.CollageTextInput.Value;
                    if (this.SexDropDownList.SelectedValue != "All")
                        userSex = this.SexDropDownList.SelectedValue;
                    if (this.StateDropDownList.SelectedValue != "")
                        StateSelect = this.StateDropDownList.SelectedValue;
                    if (this.CityDropDownList.SelectedValue != "")
                        CitySelect = this.CityDropDownList.SelectedValue;

                    NormalSearch(searchKeys, userSex, StateSelect, CitySelect, collageName);
                }
            }
            else
            {
                string userName = GetParameters("userName");//  Request.Form["userName"];
                string userSex = GetParameters("userSexRadio");// Request.Form["userSexRadio"];
                string StateSelect = GetParameters("StateSelect");// Request.Form["StateSelect"];
                string CitySelect = GetParameters("CitySelect");// Request.Form["CitySelect"];

                string EmailAddress = GetParameters("EmailAddress");//  Request.Form["EmailAddress"];
                string QQNumber = GetParameters("QQNumber");//  Request.Form["QQNumber"];
                string MSNNumber = GetParameters("MSNNumber");// Request.Form["MSNNumber"];
                string collageName = GetParameters("collageName");//  Request.Form["collageName"];
                string collageYearSelect = GetParameters("collageYearSelect");//  Request.Form["collageYearSelect"];
                string sSchoolName = GetParameters("sSchoolName");//  Request.Form["sSchoolName"];
                string sSchoolYearSelect = GetParameters("sSchoolYearSelect");// Request.Form["sSchoolYearSelect"];
                string jSchoolName = GetParameters("jSchoolName");//  Request.Form["jSchoolName"];
                string jSchoolYearSelect = GetParameters("jSchoolYearSelect");//  Request.Form["jSchoolYearSelect"];
                string lastEmployeeName = GetParameters("lastEmployeeName");//  Request.Form["lastEmployeeName"];

                if (this.CollageTextInput.Value != "请输入大学")
                    collageName = this.CollageTextInput.Value;
                if (this.SexDropDownList.SelectedValue != "All")
                    userSex = this.SexDropDownList.SelectedValue;
                if (this.StateDropDownList.SelectedValue != "")
                    StateSelect = this.StateDropDownList.SelectedValue;
                if (this.CityDropDownList.SelectedValue != "")
                    CitySelect = this.CityDropDownList.SelectedValue;

                //if (searchType == "1")
                    AdvanceSearch(userName, userSex, StateSelect, CitySelect,
                         EmailAddress, QQNumber, MSNNumber, collageName, collageYearSelect,
                         sSchoolName, sSchoolYearSelect, jSchoolName, jSchoolYearSelect,
                         lastEmployeeName);
            }
        }

        private string GetParameters(string key)
        {
            if (Request.RequestType .ToUpper() == "GET")
                return (Request[key] == null) ? "" : Request[key].ToString();
            else
                return (Request.Form[key] == null) ? "" : Request.Form[key].ToString();
        }
        #endregion


        #region 事件
        protected void SearchButton_Click(object sender, EventArgs e)
        {                                    
            string searchKeys = string.Empty;
            string collageName = string.Empty;
            string userSexRadio = string.Empty;
            string StateSelect = string.Empty;
            string CitySelect = string.Empty;

            if (this.searchKeysInputText.Value != "可输入姓名 学校 单位")
                searchKeys = searchKeysInputText.Value;
            if (this.CollageTextInput.Value != "请输入大学")
                collageName = this.CollageTextInput.Value;
            if (this.SexDropDownList.SelectedValue != "All")
                userSexRadio = this.SexDropDownList.SelectedValue;
            if (this.StateDropDownList.SelectedValue != "")
                StateSelect = this.StateDropDownList.SelectedValue;
            if (this.CityDropDownList.SelectedValue != "")
                CitySelect = this.CityDropDownList.SelectedValue;

            NormalSearch(searchKeys, userSexRadio, StateSelect, CitySelect, collageName);
            this.searchKeysInputText.Focus();
        }

        protected void MainWebPager_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            MainWebPager.CurrentPageIndex = e.NewPageIndex;
            BindMainWebPage();
        }
        

        protected void SexDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindMainWebPage();
        }

        protected void StateDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.CityDropDownList.Items.Clear();
            ListItem listItem = new ListItem("选择城市", "");
            this.CityDropDownList.Items.Add(listItem);
            if (this.StateDropDownList.SelectedValue != "")
            {
                taobaocity city = taobaocity.Find("taobaostateid ='" + this.StateDropDownList.SelectedValue + "'");
                if (city.Count > 0)
                {
                    for (int i = 0; i < city.Count; i++)
                    {
                        listItem = new ListItem(city.taobaocityid.Value, city.taobaocityid.Value);
                        this.CityDropDownList.Items.Add(listItem);
                        city.Next();
                    }
                }
            }

            BindMainWebPage();
        }

        protected void CityDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindMainWebPage();
        }

        protected void CollageTextInput_TextChanged(object sender, EventArgs e)
        {
            BindMainWebPage();
        }
        #endregion
    }
}
